﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VulnDBExplorer.Services;
using System.Text.RegularExpressions;

namespace VulnDB.Action.DataCollectors.Services.CodeAge {
	
	public interface ICodeProfile: IEnumerable<ISourceFileProfile> {
		ICodebase Codebase { get; }

		ISourceFileProfile this[string filename] { get; }
	}

	public interface ISourceFileProfile: IEnumerable<ISourceFileRevision> {
		string Filename { get; }
		string ActiveFilename { get; }
		ISourceFileRevision this[string revision] { get; }
	}

	public interface ISourceFileRevision {
		string Revision { get; }
		string Author { get; }
		int Count { get; }
		DateTime Age { get; }
		IList<FileCompareResult.FileChunk> Lines { get; }

		void consolidateLines();
	}

	public static class ISourceProfileEntryExtension {
		private static readonly Regex ProfileEntryPattern = new Regex(@"(\d+(\.\d+)*),(.+?),(\d+-\d+-\d+),(\d+): (.+)", RegexOptions.Singleline);

		public static string serializeProfiles(this ISourceFileRevision entry) {
			StringBuilder b = new StringBuilder();
			b.AppendFormat("{0},{1},{2:yyyy-MM-dd},{3}:", entry.Revision, entry.Author, entry.Age, entry.Count);
			foreach (var item in entry.Lines)
				b.AppendFormat(" {0}-{1}", item.Begin, item.End);
			return b.ToString();
		}

		public static bool deserializeProfiles(this ISourceFileRevision entry, string input) {
			Match m = ProfileEntryPattern.Match(input);
			if (m.Success) {
			}
			return m.Success;
		}
	}
}
